package old;

import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseWheelEvent;

import org.openstreetmap.osm.data.coordinates.Bounds;
import org.openstreetmap.osm.data.coordinates.EastNorth;
import org.openstreetmap.osm.data.coordinates.LatLon;
import org.openstreetmap.osm.data.projection.Projection;

public class CoordinateHelper {

	private int zoomLevel;
	private final Rectangle r;
	private final Projection p;
	private Bounds mapBounds;

	private double scaling = 1.0;
	private LatLon center;
	
	public CoordinateHelper(Projection p, Bounds mapBounds, Rectangle r, int zoomLevel) {
		this.p = p;
		this.mapBounds = mapBounds;
		this.r = r;
		this.zoomLevel = zoomLevel;
	}

	public int getZoom() {
		return zoomLevel;
	}

	public Projection getProjection() {
		return p;
	}

	public Point getPoint(EastNorth latlon2eastNorth) {
		// TODO Auto-generated method stub
		LatLon latLon = p.eastNorth2latlon(latlon2eastNorth);
		double x = latLon.lon();
		
		double minx = mapBounds.getMin().lon();
		double maxX = mapBounds.getMax().lon();
		double miny = mapBounds.getMin().lat();
		double maxy = mapBounds.getMax().lat();

		double px = (1.0 -(x-minx)/(maxX - minx)) * (double) r.width;
		
		double y = latLon.lat();
		double py = (0 + (y-miny)/(maxy - miny)) * (double) r.height;
		
		px *= scaling;
		py *= scaling;
		center = new LatLon(0.5 *(minx + maxX), 0.5 * (maxy + miny));
		
		return new Point((int)px,(int) py);
	}

	public EastNorth getCenter() {
		return p.latlon2eastNorth(center);
	}

	public Bounds getBounds() {
		return mapBounds;
	}

	public LatLon getLatLon(int i, int j) {
		// TODO Auto-generated method stub
		return null;
	}

	public int getWidth() {
		return r.width;
	}

	public int getHeight() {
		return r.height;
	}

	public void scale(double factor, MouseWheelEvent e) {
		double y = e.getPoint().x / (double) getHeight(); //They have bogus coordinate system
		double x = e.getPoint().y / (double) getWidth();
		
		double minX = mapBounds.getMin().getXCoord();
		double maxX = mapBounds.getMax().getXCoord();
		double distX = (maxX - minX);
		minX = minX + (1 - factor) * x * distX;
		maxX = maxX - (1 - factor)* (1-x) * distX;
		
		double minY = mapBounds.getMax().getYCoord();
		double maxY = mapBounds.getMin().getYCoord();
		double distY = (maxY - minY);
		minY = minY + (1 - factor) * y * distY;
		maxY = maxY - (1 - factor)* (1-y) * distY;
		
		mapBounds = new Bounds(new LatLon(minX, minY), new LatLon(maxX, maxY));
	}

	public void move(double deltaX, double deltaY) {
		double minX = mapBounds.getMin().getXCoord();
		double maxX = mapBounds.getMax().getXCoord();
		double distX = (maxX - minX);
		minX = minX - deltaY / getWidth() * distX;
		maxX = maxX - deltaY / getWidth() * distX;
		
		double minY = mapBounds.getMax().getYCoord();
		double maxY = mapBounds.getMin().getYCoord();
		double distY = (maxY - minY);
		minY = minY -  deltaX / getHeight() * distY;
		maxY = maxY -  deltaX / getHeight() * distY;
		mapBounds = new Bounds(new LatLon(minX, minY), new LatLon(maxX, maxY));
	}

}
